<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/23
 * Time: 16:44
 */

namespace App\Http\Controllers\User;

use App\Http\Classes\Author;
use App\Http\Classes\Follow;
use App\Http\Classes\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Classes\Log;

class AuthorController extends Controller
{
    public function getAuthorCard(Request $request){
        try{
            if($request->has('author_id')){
                $author_id = $request->input('author_id');
                $author = new Author($author_id);
                $data = array();
                $data['info'] = $author->infoAuthor();
                if($author_id == $_COOKIE['user_id']){
                    $data['is_self'] = true;
                }
                else{
                    $data['is_self'] = false;
                    $dataArray = array();
                    $dataArray['following_user_id'] = $_COOKIE['user_id'];
                    $dataArray['followed_user_id'] = $author_id;
                    $follow = Follow::isFollowing($dataArray);
                    $data['is_following'] = $follow;
                }
                if($data){
                    return $this->response(200,'获取成功！',$data);
                }
                else{
                    return $this->response(304,'获取失败！',$data);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function getUidInfo(Request $request){
        try{
            if($request->has('uid')){
                $uid = $request->input('uid');
                $user = new User($uid);
                $data = [];
                $data['info'] = $user->getUidInfo();
                $dataArray = [];
                $dataArray['followed_user_id'] = $uid;
                $dataArray['following_user_id'] = $_COOKIE['user_id'];
                $data['is_self'] = $uid == $_COOKIE['user_id'] ? true : false;
                if( !$data['is_self'] ){
                    $data['relationship'] = Follow::relationShip($dataArray);
                }
                if($data){
                    return $this->response(200,'获取成功！',$data);
                }
                else{
                    return $this->response(304,'获取失败！',$data);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    private function response($status,$message,$data){
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;
        return response()->json($response);
    }
}